<?php
namespace app\model;

use think\Model;
use app\model\UserFans;
use app\model\Bbs;

/**
 * 用户
 */
class User extends Model
{
    protected $table = "ea_user";

    public static function setWhere($data)
    {
        $model = self::whereNull('delete_time');

        if(isset($data['id']) && $data['id']){
            $mdoel = $model->where('id', $data['id']);
        }

        return $model;
    }

    /**
     * [getUserInfo 获取用户信息]
     * @since  2022-03-21
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getUserInfo($data)
    {
        $info = self::setWhere($data)->findOrEmpty()->toArray();

        return $info;
    }

    /**
     * [getUserInfo 获取用户信息,主要用于圈子首页,详细,]
     * @since  2022-03-21
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getUserHomeInfo($data)
    {
        $info = self::setWhere($data)
            ->field('id, nickname, avatar, descs')
            ->withCount(['follow', 'fans', 'bbs'])
            ->find();

        return $info;
    }

    /**
     * [follow 我关注的用户]
     * @since  2022-04-08
     * @author Dai哥
     * @return [type]     [description]
     */
    public function follow()
    {
        return $this->hasMany(UserFans::class, 'user_id', 'id');
    }

    /**
     * [follow 我的粉丝]
     * @since  2022-04-08
     * @author Dai哥
     * @return [type]     [description]
     */
    public function fans()
    {
        return $this->hasMany(UserFans::class, 'fans_id', 'id');
    }

    /**
     * [follow 我的动态]
     * @since  2022-04-08
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbs()
    {
        return $this->hasMany(Bbs::class, 'user_id', 'id');
    }
}